<?php


namespace app\controller\admin;


use app\model\config\CouponModel;
use app\model\config\RegionModel;
use app\service\config\ConfigService;

class ConfigController extends BaseAdminController
{

    function setModel()
    {
        return new CouponModel();
    }

    function setWithModel()
    {
        // TODO: Implement setWithModel() method.
    }

    function setSearch()
    {
        // TODO: Implement setSearch() method.
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    /**
     * 网站配置
     * @return \think\response\Json
     */
    public function setConfig()
    {
        try {
            $params = $this->request->param();
            foreach ($params as $k => $v) {
                ConfigService::setConfigValue($k, $v);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取配置信息
     * @return \think\response\Json
     */
    public function getConfigInfo()
    {
        try {
            $type = $this->request->param('type', 0);
            $list = ConfigService::getTypeConfigValueArray($type);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取一个配置
     * @return \think\response\Json
     */
    public function getConfigInfoByName()
    {
        try {
            $name = $this->request->param('name', '', 'trim');
            $configInfo = ConfigService::getConfigValue($name);
            return $this->success($configInfo);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取城市列表
     * @return \think\response\Json
     */
    public function getCityList()
    {
        try {
            $list = RegionModel::where('level', 2)->field('id,name')->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取城市列表
     * @return \think\response\Json
     */
    public function getOpenCityList()
    {
        try {
            $list = ConfigService::getConfigValue('open_city');
            $list1 = [];
            foreach ($list as $k => $value) {
                $list1[$k] = [
                    'id' => $value,
                    'name' => RegionModel::where('id', $value)->value('name')
                ];
            }
            return $this->success($list1);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}